#include<bits/stdc++.h>
#define ll long long
#define L(x) xd[x].l
#define R(x) xd[x].r
#define mid (l+r>>1)
#define lc(x) L(x),l,mid
#define rc(x) R(x),mid+1,r
#define OK Ll<=l&&r<=Rr
#define Root 1,1,n
#define rep(x,y,z) for(int x=(y);x<=(z);x++)
#define per(x,y,z) for(int x=(y);x>=(z);x--)
#define repn(x) rep(x,1,n)
#define repm(x) rep(x,1,m)
#define pb push_back
#define e(x) for(int i=h[x],y=to[i];i;i=nxt[i],y=to[i])
#define E(x) for(auto y:p[x])
#define Pi pair<int,int>
#define ui unsigned ll
inline int read(){int s=0,w=1;char c=getchar();while(c<48||c>57) {if(c=='-') w=-1;c=getchar();}while(c>=48&&c<=57)s=(s<<1)+(s<<3)+c-48,c=getchar();return s*w;}
inline void pf(ll x){if(x<0) putchar('-'),x=-x;if(x>9)pf(x/10);putchar(x%10+48);}
using namespace std;
const int N =1e5+5,M=2e6+5,inf=(1LL<<30)-1,mod=998244853;
const ll llf=1e18;
inline void add(int &a,int b){((a+=b)>=mod) and (a-=mod);}
inline int Add(int a,int b){return add(a,b),a;}
inline int mul(int a,int b){return 1LL*a*b%mod;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline void red(int &a,int b){add(a,mod-b);}
inline int Red(int a,int b){return red(a,b),a;}
inline int qp(int a,int b){if(!b)return 1;int c=qp(a,b>>1);Mul(c,c);if(b&1)Mul(c,a);return c;}
inline int INV(int x){return qp(x,mod-2);}
int n,m;
struct node{
	int l,r;
}a[N];
ll dp[N];
inline int get(int l,int r,int Ll,int Rr){
	if(r<Ll||l>Rr)return 0;
	return min(r,Rr)-max(l,Ll)+1;
}
struct seg{
	int l,r;
	ll sl,sr,w;
}xd[M];
int tot,root[N];
inline void modify(int x,int l,int r,int p,int Ll,int Rr){
	xd[x].w++,xd[x].sl+=Ll,xd[x].sr+=Rr;
	if(l==r)return;
	if(p<=mid)xd[++tot]=xd[L(x)],L(x)=tot,modify(lc(x),p,Ll,Rr);
	else xd[++tot]=xd[R(x)],R(x)=tot,modify(rc(x),p,Ll,Rr); 
}
inline seg query(int X,int x,int l,int r,int Ll,int Rr){
	Ll=max(Ll,1);	
	if(Ll>Rr)return {0,0,0,0,0};
	if(OK)return {0,0,xd[x].sl-xd[X].sl,xd[x].sr-xd[X].sr,xd[x].w-xd[X].w};
	if(Rr<=mid)return query(L(X),lc(x),Ll,Rr);
	if(Ll>mid)return query(R(X),rc(x),Ll,Rr);
	seg nl=query(L(X),lc(x),Ll,Rr),nr=query(R(X),rc(x),Ll,Rr);
	nl.sl+=nr.sl,nl.sr+=nr.sr,nl.w+=nr.w;
	return nl;
}
inline seg Get(int l,int r,int Ll,int Rr){
	if(l>r||Ll>Rr)return {0,0,0,0,0};
	Ll=max(Ll,1);
	return query(root[Ll-1],root[Rr],1,n,l,r);
}
inline ll w(int l,int r){
	ll ans=0;
	ans+=Get(1,l,r,n).w;
	seg nw=Get(l,n,1,r);
	ans+=1LL*(r-l)*nw.w+nw.sl-nw.sr;
	nw=Get(1,l-1,l,mid-((l+r)%2==0));
	ans+=nw.sr+nw.w*(1-l);
	nw=Get(1,l-1,mid-((l+r)%2==0)+1,r);
	ans+=nw.w*r-nw.sr;
	nw=Get(l,mid,r+1,n);
	ans+=nw.sl-nw.w*l;
	nw=Get(mid+1,r,r+1,n);
	ans+=nw.w*(r+1)-nw.sl;
	return ans;
}
struct nod{
	int x,l,r;
}q[N];
int h=1,t;
inline ll got(int l,int r){
	return dp[l]+w(l,r-1);
}
vector<int>p[N];
inline void subtaskall(){
	repm(i)p[a[i].r].pb(a[i].l);
	repn(r){
		xd[++tot]=xd[root[r-1]],root[r]=tot;
		for(auto l:p[r])modify(root[r],1,n,l,l,r);
	}
	repn(i)dp[i]=llf;
	q[++t]={0,1,n};
	repn(i){
		while(h<=t&&q[h].r<i)h++;
		q[h].l=max(q[h].l,i+1),dp[i]=got(q[h].x,i);
		while(h<=t){
			ll wn=got(i,q[t].l),wp=got(q[t].x,q[t].l);
			if(wn<=wp)t--;
			else break;
		}
		if(h<=t){
			int l=q[t].l,r=n,ans=n+1;
			while(l<=r)if(got(i,mid)<=got(q[t].x,mid))ans=mid,r=mid-1;
			else l=mid+1;
			if(ans<=n)q[t].r=ans-1,q[++t]={i,ans,n};
		}
		else q[++t]={i,i+1,n};
	}
	cout <<dp[n];
}
inline void Main(){
	n=read(),m=read();
	repm(i)a[i].l=read(),a[i].r=read()-1;
	subtaskall();
}
signed main(){
	freopen("block.in","r",stdin);
	freopen("block.out","w",stdout);
	int T=1;
	while(T--)Main();
	return 0;
}
